/**
 * @file listePersonneAjout.c
 * @brief Fichier source du module listePersonneAjout.
 *
 * @author Rachid Delory, Mamadou Keita, Florian Bernard
 */

/* *********************************************************************

                       Librairies de listePersonneAjout

********************************************************************* */

#include "listePersonneAjout.h"

/* *********************************************************************

                        Fonctions                                      

********************************************************************* */

void ajoutPersonneListe(GtkListStore *liste, Ensemble *ens)
{
  GtkTreeIter iter;
  int taille, i;
  taille = getTaille_Ens(ens);
  prems_Ens(ens);
  for(i = 0; i < taille; i++)
    {
      gtk_list_store_append (liste, &iter);
      gtk_list_store_set (liste, &iter,
                          ID_COLUMN_GLPA, getId_Per(getData_Ens(ens)),
                          PRENOM_COLUMN_GLPA, getPrenom_Per(getData_Ens(ens)),
			  NOM_COLUMN_GLPA, getNom_Per(getData_Ens(ens)),
                          LIE_COLUMN_GLPA,  FALSE,
                          -1);
      suivant_Ens(ens);
    }
}

void reponseDialogueCB_GLpa(GtkDialog *dialog, gint response_id,
		       gpointer data)
{
  GtkListStore *liste = data;
  GtkTreeIter iter;
  if(response_id == GTK_RESPONSE_ACCEPT)
    {
      GtkWidget *vbox;
      GtkWidget *contentArea;
      GList *glist;
      Personne gen;
      contentArea = gtk_dialog_get_content_area(dialog);
      glist = gtk_container_get_children(GTK_CONTAINER(contentArea));
      vbox = g_list_nth_data(glist, 0);
      g_list_free_1(glist);
      glist = gtk_container_get_children(GTK_CONTAINER(vbox));
      contentArea = g_list_nth_data(glist, 1);
      g_list_free(glist);
      init_Per(&gen);
      setPrenom_Per(&gen, gtk_entry_get_text(GTK_ENTRY(contentArea)));
      insertion_DPer(&gen);
      gtk_list_store_append (liste, &iter);
      gtk_list_store_set (liste, &iter,
                          ID_COLUMN_GLPA, getId_Per(&gen),
                          PRENOM_COLUMN_GLPA, getPrenom_Per(&gen),
			  NOM_COLUMN_GLPA, getNom_Per(&gen),
                          LIE_COLUMN_GLPA,  FALSE,
                          -1);
      liberer_Per(&gen);
    }
  gtk_widget_destroy(GTK_WIDGET(dialog));
}

void ajouterPersonneCb(GtkButton *btn, gpointer data)
{
  GtkWidget *widget, *contentArea;
  GtkWidget *vbox;
  GtkWidget *label;
  GtkWidget *entry;
  
  widget = gtk_dialog_new_with_buttons("Création personne",
				       NULL,
				       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				       GTK_STOCK_SAVE,
				       GTK_RESPONSE_ACCEPT,
				       GTK_STOCK_CANCEL,
				       GTK_RESPONSE_REJECT,
				       NULL);
  gtk_window_set_default_size(GTK_WINDOW(widget), 400, 300);
  contentArea = gtk_dialog_get_content_area(GTK_DIALOG(widget));
  vbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_container_add(GTK_CONTAINER(contentArea), vbox);
  
  gtk_box_set_child_packing(GTK_BOX(contentArea), vbox, TRUE, TRUE, 1, GTK_PACK_START);

  label = gtk_label_new("Personne");
  gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 1);
  entry = gtk_entry_new();
  gtk_box_pack_start(GTK_BOX(vbox), entry, TRUE, TRUE, 1);
  gtk_widget_show_all(widget);

  g_signal_connect(widget, "response", G_CALLBACK(reponseDialogueCB_GLpa),data); 
  (void)btn;
}

void supprimerPersonneCb(GtkButton *btn, gpointer data)
{
  GtkWidget * view = data;
  GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
  GtkTreeIter iter;
  GtkTreeModel *model;
  guint id;
  if(gtk_tree_selection_get_selected(selection, &model, &iter))
    {
      gtk_tree_model_get(model, &iter, ID_COLUMN_GLPA, &id, -1);
      suppression_DPer(id);
      gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
    }
  (void) btn;
}

void modifierReponseDialogueCB_GLpa(GtkDialog *dialog, gint response_id,
		       gpointer data)
{
  GList *list = data;
  Personne *per;
  GtkTreeIter *iter;
  GtkTreeModel *model;
  per = g_list_nth_data(list, 0);
  iter = g_list_nth_data(list, 1);
  model = g_list_nth_data(list, 2);

  if(response_id == GTK_RESPONSE_ACCEPT)
    {
      GtkWidget *content;
      content = g_list_nth_data(list, 3);
      setPrenom_Per(per, gtk_entry_get_text(GTK_ENTRY(content)));
      content = g_list_nth_data(list, 4);
      setNom_Per(per, gtk_entry_get_text(GTK_ENTRY(content)));
      content = g_list_nth_data(list, 5);
      setNomScene_Per(per, gtk_entry_get_text(GTK_ENTRY(content)));
      content = g_list_nth_data(list, 6);
      setLieuxNaissance_Per(per, gtk_entry_get_text(GTK_ENTRY(content)));
      content = g_list_nth_data(list, 7);
      setSite_Per(per, gtk_entry_get_text(GTK_ENTRY(content)));
      setMaj_Per(per, 1);
      insertion_DPer(per);
      gtk_list_store_set(GTK_LIST_STORE(model), iter,
			 ID_COLUMN_GLPA, getId_Per(per),
			 PRENOM_COLUMN_GLPA, getPrenom_Per(per),
			 NOM_COLUMN_GLPA, getNom_Per(per),
                          -1);
      gtk_tree_iter_free(iter);
    }
  g_list_free(list);
  if(per)
    detruire_Per(&per);
  gtk_widget_destroy(GTK_WIDGET(dialog));
}

void modifierPersonne(const int id, GtkTreeIter *iter, GtkTreeModel *model)
{
  GtkWidget *widget, *contentArea;
  GtkWidget *vbox;
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *entry;
  Personne *personne = NULL;
  Ensemble ens;
  GList *list = NULL;
  init_Ens(&ens);
  recuperation_DPer(&ens, id, SEUL_DPER);
  if(getTaille_Ens(&ens) != 1)
    {
      libere_Ens(&ens, detruire_Per);
      return;
    }
  prems_Ens(&ens);
  copier_Per(&personne, getData_Ens(&ens));
  libere_Ens(&ens, detruire_Per);
  list = g_list_append(list, personne);
  list = g_list_append(list, iter);
  list = g_list_append(list, model);

  widget = gtk_dialog_new_with_buttons("Création personne",
				       NULL,
				       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				       GTK_STOCK_SAVE,
				       GTK_RESPONSE_ACCEPT,
				       GTK_STOCK_CANCEL,
				       GTK_RESPONSE_REJECT,
				       NULL);
  contentArea = gtk_dialog_get_content_area(GTK_DIALOG(widget));
  vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_container_add(GTK_CONTAINER(contentArea), vbox);
  
  gtk_box_set_child_packing(GTK_BOX(contentArea), vbox, TRUE, TRUE, 1, GTK_PACK_START);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Prénom");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  entry = gtk_entry_new();
  list = g_list_append(list, entry);
  gtk_entry_set_text(GTK_ENTRY(entry), (getPrenom_Per(personne) == NULL ? "" : getPrenom_Per(personne)));
  gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Nom");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  entry = gtk_entry_new();
  list = g_list_append(list, entry);
  gtk_entry_set_text(GTK_ENTRY(entry), (getNom_Per(personne) == NULL ? "" : getNom_Per(personne)));
  gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 1);
  gtk_widget_show_all(widget);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Nom de scène");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  entry = gtk_entry_new();
  list = g_list_append(list, entry);
  gtk_entry_set_text(GTK_ENTRY(entry), (getNomScene_Per(personne) == NULL ? "" : getNomScene_Per(personne)));
  gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Lieux de naissance");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  entry = gtk_entry_new();
  list = g_list_append(list, entry);
  gtk_entry_set_text(GTK_ENTRY(entry), (getLieuxNaissance_Per(personne) == NULL ? "" : getLieuxNaissance_Per(personne)));
  gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Site internet");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  entry = gtk_entry_new();
  list = g_list_append(list, entry);
  gtk_entry_set_text(GTK_ENTRY(entry), (getSite_Per(personne) == NULL ? "" : getSite_Per(personne)));
  gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Date de naissance");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Date de décès");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);

  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  label = gtk_label_new("Biographie");
  gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 1);
  entry = gtk_text_view_new();
  {
    GtkTextBuffer *buf;
    buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry));
    gtk_text_buffer_set_text(buf, (getBiographie_Per(personne) == NULL ? "" : getBiographie_Per(personne)), (getBiographie_Per(personne) == NULL ? 0 : strlen(getBiographie_Per(personne))));
    
    gtk_text_view_set_buffer(GTK_TEXT_VIEW(entry), buf);
  }
  hbox = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(hbox), entry);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, TRUE, 1);
  gtk_widget_show_all(widget);

  g_signal_connect(widget, "response", G_CALLBACK(modifierReponseDialogueCB_GLpa), list); 
}

void modifierPersonneCb(GtkButton *btn, gpointer data)
{
  GtkWidget *view = data;
  GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
  guint id;
  GtkTreeIter iter;
  GtkTreeModel *model;

  if(gtk_tree_selection_get_selected(selection, &model, &iter))
    {
      gtk_tree_model_get(model, &iter, ID_COLUMN_GLPA, &id, -1);
      modifierPersonne(id, gtk_tree_iter_copy(&iter), model);
    }
  (void)btn;
}

void toggledLie_Per(GtkCellRendererToggle *cell_renderer, gchar *gpath, gpointer user_data)
{
  GtkTreePath * path = gtk_tree_path_new_from_string(gpath);
  GList *glist = user_data;
  AjoutPane *pane;
  GtkListStore *store;
  GtkTreeIter iter;
  gboolean b;
  guint id;
  pane = g_list_nth_data(glist, 0);
  store = g_list_nth_data(glist, 1);
  if(path)
    {
      Ensemble *ens = NULL;
      gtk_tree_model_get_iter (GTK_TREE_MODEL(store), &iter, path);
      gtk_tree_path_free (path);

      gtk_tree_model_get (GTK_TREE_MODEL(store), &iter,
			  ID_COLUMN_GLPA, &id,
			  LIE_COLUMN_GLPA, &b, 
			  -1);
      gtk_list_store_set(store, &iter, LIE_COLUMN_GLPA, !b, -1);
      if(pane->oeuvre)
	ens = getEnsembleCollaborateur_Oeu(pane->oeuvre);

      if(!b)
	{
	  /* Ajout du personne */
	  Activite act;
	  Personne *per = creer_Per();
	  init_Act(&act);
	  recuperationOeuvre_DPer(&per, id);
	  setPersonne_Act(&act, per);
	  insert_Ens(ens, &act, creer_Act, copier_Act);
	  setMaj_Ens(ens, 1);
	  detruire_Per(&per);
	}
      else
	{
	  int tailleEns = getTaille_Ens(ens);
	  int i;
	  prems_Ens(ens);
	  for(i = 0; i < tailleEns; i++)
	    {
	      if(getId_Per(getPersonne_Act(getData_Ens(ens))) == id)
		{
		  setSup_Ens(ens, 1);
		}
	      suivant_Ens(ens);
	    }
	}
    }
  
  (void)cell_renderer;
}

void checkPersonne_GLpa(Ensemble *personne, GtkWidget *widget)
{
  GtkWidget *view;
  GtkWidget *wt;
  GList *glist;
  GtkTreeModel *store;
  GtkTreeIter iter;
  gboolean valid;
  gboolean b;
  guint id;
  int i, tailleEns;
  glist = gtk_container_get_children(GTK_CONTAINER(widget));
  wt = g_list_nth_data(glist, 0);
  g_list_free(glist);
  glist = gtk_container_get_children(GTK_CONTAINER(wt));
  wt = g_list_nth_data(glist, 0);
  g_list_free(glist);

  glist = gtk_container_get_children(GTK_CONTAINER(wt));
  view = g_list_nth_data(glist, 0);
  g_list_free(glist);

  store = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
  valid = gtk_tree_model_get_iter_first(store, &iter);
  
  if(personne)
    tailleEns = getTaille_Ens(personne);
  else
    tailleEns = 0;

  while(valid)
    {
      
      gtk_tree_model_get(store, &iter, ID_COLUMN_GLPA, &id, -1);
      
      b = FALSE;
      
      if(personne)
	{
	  prems_Ens(personne);
	  for(i = 0; i < tailleEns; i++)
	    {
	      if(getId_Per(getPersonne_Act(getData_Ens(personne))) == id)
		{
		  b = TRUE;
		  break;
		}
	      suivant_Ens(personne);
	    }
	}
      gtk_list_store_set(GTK_LIST_STORE(store), &iter, LIE_COLUMN_GLPA, b, -1);
      valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
    }
}

void testament_GLpa(GtkWidget *object, gpointer user_data)
{
  GList *glist = user_data;
  if(glist)
    g_list_free(glist);
  (void)object;
}

GtkWidget * creer_GLpa(AjoutPane *pane)
{
  GtkTreeViewColumn *column;
  GtkCellRenderer *renderer;
  GtkListStore *liste;
  GtkTreeSelection *select;
  GtkWidget *view;
  GtkWidget *btn;
  GtkWidget *vbox, *hbox;
  GtkWidget *scroll;
  GList *glist = NULL;
  Ensemble *ens = creer_Ens();
  
  recuperation_DPer(ens, 0, TOUS_DPER);

  liste = gtk_list_store_new(N_COLUMN_GLPA,
			     G_TYPE_BOOLEAN,
			     G_TYPE_STRING,
			     G_TYPE_STRING,
			     G_TYPE_INT
			      );
  view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(liste));
  renderer = gtk_cell_renderer_toggle_new();
  column = gtk_tree_view_column_new_with_attributes("Lié",
						    renderer,
						    "active",
						    LIE_COLUMN_GLPA,
						    NULL);
  glist = g_list_append(glist, pane);
  glist = g_list_append(glist, liste);
  g_signal_connect(renderer, "toggled", G_CALLBACK(toggledLie_Per), glist);

  gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
  
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes("Prénom",
						    renderer,
						    "text",
						    PRENOM_COLUMN_GLPA,
						    NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);

  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes("Nom",
						    renderer,
						    "text",
						    NOM_COLUMN_GLPA,
						    NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);

  
  renderer = gtk_cell_renderer_text_new();
  column = gtk_tree_view_column_new_with_attributes("ID",
						    renderer,
						    "text",
						    ID_COLUMN_GLPA,
						    NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
  
  select = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
  gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
  
  scroll = gtk_scrolled_window_new(NULL, NULL);
  
  gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), view);
  
  vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), scroll, TRUE, TRUE, 1);
  hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1);
  gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 1);
  
  btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
  gtk_box_pack_start(GTK_BOX(hbox), btn, FALSE, FALSE, 1);
  g_signal_connect(btn, "clicked", G_CALLBACK(ajouterPersonneCb), liste);
  btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
  gtk_box_pack_start(GTK_BOX(hbox), btn, FALSE, FALSE, 1);
  g_signal_connect(btn, "clicked", G_CALLBACK(supprimerPersonneCb), view);

  btn = gtk_button_new_from_stock(GTK_STOCK_EDIT);
  gtk_box_pack_start(GTK_BOX(hbox), btn, FALSE, FALSE, 1);
  g_signal_connect(btn, "clicked", G_CALLBACK(modifierPersonneCb), view);

  ajoutPersonneListe(liste, ens);
  detruire_Ens(&ens, detruire_Per);
  g_signal_connect(view, "destroy", G_CALLBACK(testament_GLpa), glist);
  return vbox;
}
